using BuildingBlocks.Core.Entities;
using Identity.Domain.ObjectValue;

namespace Identity.Domain.ValueObjects
{
    public class PasswordHash : ValueObject
    {
        public string Value { get; }

        private PasswordHash() { } // EF Core 或 ORM 使用

        private PasswordHash(string value)
        {
            if (string.IsNullOrWhiteSpace(value))
                throw new ArgumentException("Password hash cannot be empty.");

            Value = value;
        }

        public static PasswordHash FromPlainText(string plainPassword)
        {
            var hash = HashUtility.Hash(plainPassword);
            return new PasswordHash(hash);
        }

        public bool Verify(string plainPassword)
        {
            return HashUtility.Verify(plainPassword, Value);
        }

        protected override IEnumerable<object> GetEqualityComponents()
        {
            yield return Value;
        }

        public override string ToString() => Value;
    }
}